//https://leetcode.cn/problems/ones-and-zeroes/
package codeRandomThoughts.Test474一和零;

public class Solution2 {
    public int findMaxForm(String[] strs, int m, int n) {
        int len = strs.length;
        int[] countZero = new int[len];
        int[] countOne = new int[len];

        //初始化两个数组
        for (int i = 0; i < strs.length; i++) {
            countZero[i] = getZeroCount(strs[i]);
            countOne[i] = strs[i].length() - countZero[i];
        }

        //要求的是最大子集的长度
        //最多有m个0和n个1的strs的最大子集的大小
        int[][] dp = new int[m + 1][n + 1];

        for (int i = 0; i < strs.length; i++) {
            for (int j = m; j >= countZero[i]; j--) {
                for (int k = n; k >= countOne[i]; k--) {
                    dp[j][k] = Math.max(dp[j][k], dp[j - countZero[i]][k - countOne[i]] + 1);
                }
            }
        }

        return dp[m][n];
    }

    public int getZeroCount(String s) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '0') count++;
        }
        return count;
    }
}